package com.leinao.media.callback;

import com.leinao.media.sdk.hk.HCNetSDK;
import com.leinao.media.service.RedisService;
import com.sun.jna.Pointer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 实时测温回调函数
 *
 * @author zhangyu
 */
@Slf4j
@Component
public class RealTimeThermometryCallBack implements HCNetSDK.FRemoteConfigCallback {

    @Override
    public void invoke(int dwType, Pointer lpBuffer, int dwBufLen, Pointer pUserData) {
        log.info("长连接回调获取数据,NET_SDK_CALLBACK_TYPE_STATUS:{}", dwType);
        switch (dwType) {
            case 0:
                // 4字节dwStatus状态值+4字节错误号
                HCNetSDK.BYTE_ARRAY struCallbackStatus = new HCNetSDK.BYTE_ARRAY(8);
                struCallbackStatus.write();
                Pointer pStatus = struCallbackStatus.getPointer();
                pStatus.write(0, lpBuffer.getByteArray(0, struCallbackStatus.size()), 0, dwBufLen);
                struCallbackStatus.read();

                int iStatus = 0;

                for (int i = 0; i < 4; i++) {
                    int offSet = i * 8;
                    int iByte = struCallbackStatus.byValue[i] & 0xff;
                    iStatus = iStatus + (iByte << offSet);
                }

                switch (iStatus) {
                    // NET_SDK_CALLBACK_STATUS_SUCCESS
                    case 1000:
                        log.info("实时温度检测成功,dwStatus:{}", iStatus);
                        break;
                    case 1001:
                        log.info("正在实时温度检测中,dwStatus:{}", iStatus);
                        break;
                    case 1002:
                        int iErrorCode = 0;
                        for (int i = 0; i < 4; i++) {
                            int offSet = i * 8;
                            int iByte = struCallbackStatus.byValue[i + 4] & 0xff;
                            iErrorCode = iErrorCode + (iByte << offSet);
                        }
                        log.info("实时温度检测失败, dwStatus:{},错误号:{}", iStatus, iErrorCode);
                        break;
                }
                break;
            case 2: //NET_SDK_CALLBACK_TYPE_DATA
                HCNetSDK.NET_DVR_THERMOMETRY_UPLOAD m_struThermometryInfo = new HCNetSDK.NET_DVR_THERMOMETRY_UPLOAD();
                m_struThermometryInfo.write();
                Pointer pThermometryInfo = m_struThermometryInfo.getPointer();
                pThermometryInfo.write(0, lpBuffer.getByteArray(0, m_struThermometryInfo.size()), 0, m_struThermometryInfo.size());
                m_struThermometryInfo.read();

                String strTimeYear = Integer.toString((m_struThermometryInfo.dwAbsTime >> 26) + 2000);
                String strTimeMonth = Integer.toString((m_struThermometryInfo.dwAbsTime >> 22) & 15);
                String strTimeDay = Integer.toString((m_struThermometryInfo.dwAbsTime >> 17) & 31);
                String strTimeHour = Integer.toString((m_struThermometryInfo.dwAbsTime >> 12) & 31);
                String strTimeMinute = Integer.toString((m_struThermometryInfo.dwAbsTime >> 6) & 63);
                String strTimeSecond = Integer.toString((m_struThermometryInfo.dwAbsTime) & 63);

                String strTime = strTimeYear + "-" + strTimeMonth + "-" + strTimeDay + " " +
                        strTimeHour + ":" + strTimeMinute + ":" + strTimeSecond;

                // 点测温
                if (m_struThermometryInfo.byRuleCalibType == 0) {
                    log.info("-----点测温获取温度如下-----");
                    log.info("获取的实时温度信息,时间:{},规则ID:{},规则类型:{}", strTime, m_struThermometryInfo.byRuleID, m_struThermometryInfo.byRuleCalibType);
                    log.info("点测温当前温度:{}", m_struThermometryInfo.struPointThermCfg.fTemperature);
                }
                // 框测温
                if ((m_struThermometryInfo.byRuleCalibType == 1) && (m_struThermometryInfo.struLinePolygonThermCfg.fMaxTemperature > 0)) {
                    log.info("-----框测温获取温度如下-----");
                    log.info("获取的实时温度信息,时间:{},规则ID:{},规则类型:{}", strTime, m_struThermometryInfo.byRuleID, m_struThermometryInfo.byRuleCalibType);
                    log.info("框测温最高温度:{}", m_struThermometryInfo.struLinePolygonThermCfg.fMaxTemperature);
                    log.info("框测温最低温度:{}", m_struThermometryInfo.struLinePolygonThermCfg.fMinTemperature);
                    log.info("框测温平均温度:{}", m_struThermometryInfo.struLinePolygonThermCfg.fAverageTemperature);
                    log.info("框测温温差:{}", m_struThermometryInfo.struLinePolygonThermCfg.fTemperatureDiff);
                }
                // 线测温
                if (m_struThermometryInfo.byRuleCalibType == 2) {
                    log.info("-----线测温获取温度如下-----");
                    log.info("获取的实时温度信息,时间:{},规则ID:{},规则类型:{}", strTime, m_struThermometryInfo.byRuleID, m_struThermometryInfo.byRuleCalibType);
                    log.info("线测温最高温度:{}", m_struThermometryInfo.struLinePolygonThermCfg.fMaxTemperature);
                    log.info("线测温最低温度:{}", m_struThermometryInfo.struLinePolygonThermCfg.fMinTemperature);
                    log.info("线测温平均温度:{}", m_struThermometryInfo.struLinePolygonThermCfg.fAverageTemperature);
                    log.info("线测温温差:{}", m_struThermometryInfo.struLinePolygonThermCfg.fTemperatureDiff);
                }
                break;
            default:
                break;
        }
    }
}